<template>
  <div class="visualization-container">
    <el-row :gutter="20" class="header">
      <el-col :span="24">
        <h1>数据可视化</h1>
        <p>校园二手交易系统数据统计与分析</p>
      </el-col>
    </el-row>

    <el-row :gutter="20" class="stats-cards">
      <el-col :span="6">
        <el-card class="stat-card">
          <div class="stat-content">
            <div class="stat-icon bg-blue">
              <el-icon><Goods /></el-icon>
            </div>
            <div class="stat-info">
              <div class="stat-number">{{ productCount }}</div>
              <div class="stat-label">商品总数</div>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="stat-card">
          <div class="stat-content">
            <div class="stat-icon bg-green">
              <el-icon><ShoppingCart /></el-icon>
            </div>
            <div class="stat-info">
              <div class="stat-number">{{ orderCount }}</div>
              <div class="stat-label">订单总数</div>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="stat-card">
          <div class="stat-content">
            <div class="stat-icon bg-orange">
              <el-icon><User /></el-icon>
            </div>
            <div class="stat-info">
              <div class="stat-number">{{ userCount }}</div>
              <div class="stat-label">用户总数</div>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="stat-card">
          <div class="stat-content">
            <div class="stat-icon bg-red">
              <el-icon><TrendCharts /></el-icon>
            </div>
            <div class="stat-info">
              <div class="stat-number">{{ salesAmount }}</div>
              <div class="stat-label">总销售额(元)</div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20" class="charts-row">
      <el-col :span="12">
        <el-card class="chart-card">
          <template #header>
            <div class="card-header">
              <span>商品分类统计</span>
            </div>
          </template>
          <div ref="categoryChartRef" class="chart-container"></div>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card class="chart-card">
          <template #header>
            <div class="card-header">
              <span>月度销售趋势</span>
            </div>
          </template>
          <div ref="salesChartRef" class="chart-container"></div>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20" class="charts-row">
      <el-col :span="24">
        <el-card class="chart-card">
          <template #header>
            <div class="card-header">
              <span>热门商品排行</span>
            </div>
          </template>
          <div ref="topProductsChartRef" class="chart-container"></div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue'
import * as echarts from 'echarts'
import { Goods, ShoppingCart, User, TrendCharts } from '@element-plus/icons-vue'

// 数据统计
const productCount = ref(128)
const orderCount = ref(356)
const userCount = ref(89)
const salesAmount = ref(12800)

// 图表引用
const categoryChartRef = ref(null)
const salesChartRef = ref(null)
const topProductsChartRef = ref(null)

// 图表实例
let categoryChart = null
let salesChart = null
let topProductsChart = null

// 初始化图表
const initCharts = () => {
  // 商品分类统计图表
  if (categoryChartRef.value) {
    categoryChart = echarts.init(categoryChartRef.value)
    const categoryOption = {
      title: {
        text: '商品分类占比',
        left: 'center'
      },
      tooltip: {
        trigger: 'item'
      },
      legend: {
        orient: 'vertical',
        left: 'left'
      },
      series: [
        {
          name: '商品分类',
          type: 'pie',
          radius: '50%',
          data: [
            { value: 45, name: '电子产品' },
            { value: 30, name: '书籍资料' },
            { value: 25, name: '生活用品' },
            { value: 15, name: '服装配饰' },
            { value: 13, name: '其他' }
          ],
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }
      ]
    }
    categoryChart.setOption(categoryOption)
  }

  // 月度销售趋势图表
  if (salesChartRef.value) {
    salesChart = echarts.init(salesChartRef.value)
    const salesOption = {
      title: {
        text: '近6个月销售趋势'
      },
      tooltip: {
        trigger: 'axis'
      },
      xAxis: {
        type: 'category',
        data: ['1月', '2月', '3月', '4月', '5月', '6月']
      },
      yAxis: {
        type: 'value'
      },
      series: [
        {
          data: [120, 200, 150, 80, 70, 110],
          type: 'line',
          smooth: true
        }
      ]
    }
    salesChart.setOption(salesOption)
  }

  // 热门商品排行图表
  if (topProductsChartRef.value) {
    topProductsChart = echarts.init(topProductsChartRef.value)
    const topProductsOption = {
      title: {
        text: '热门商品销量排行'
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'shadow'
        }
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
      },
      xAxis: {
        type: 'value',
        boundaryGap: [0, 0.01]
      },
      yAxis: {
        type: 'category',
        data: ['iPhone 12', 'MacBook Pro', '数据结构教材', '篮球', '耳机']
      },
      series: [
        {
          type: 'bar',
          data: [182, 156, 120, 98, 76]
        }
      ]
    }
    topProductsChart.setOption(topProductsOption)
  }
}

// 窗口大小改变时重置图表大小
const resizeCharts = () => {
  if (categoryChart) categoryChart.resize()
  if (salesChart) salesChart.resize()
  if (topProductsChart) topProductsChart.resize()
}

// 组件挂载时初始化图表
onMounted(() => {
  // 模拟数据加载
  setTimeout(() => {
    initCharts()
    window.addEventListener('resize', resizeCharts)
  }, 100)
})

// 组件卸载前销毁图表
onBeforeUnmount(() => {
  if (categoryChart) {
    categoryChart.dispose()
    categoryChart = null
  }
  if (salesChart) {
    salesChart.dispose()
    salesChart = null
  }
  if (topProductsChart) {
    topProductsChart.dispose()
    topProductsChart = null
  }
  window.removeEventListener('resize', resizeCharts)
})
</script>

<style scoped>
.visualization-container {
  padding: 20px;
  background-color: #f5f5f5;
  min-height: calc(100vh - 60px);
}

.header {
  margin-bottom: 20px;
  text-align: center;
}

.header h1 {
  font-size: 28px;
  color: #333;
  margin-bottom: 10px;
}

.header p {
  font-size: 16px;
  color: #666;
}

.stats-cards {
  margin-bottom: 20px;
}

.stat-card {
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.stat-content {
  display: flex;
  align-items: center;
}

.stat-icon {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 15px;
}

.stat-icon el-icon {
  font-size: 24px;
  color: white;
}

.bg-blue {
  background-color: #409eff;
}

.bg-green {
  background-color: #67c23a;
}

.bg-orange {
  background-color: #e6a23c;
}

.bg-red {
  background-color: #f56c6c;
}

.stat-info {
  flex: 1;
}

.stat-number {
  font-size: 24px;
  font-weight: bold;
  color: #333;
}

.stat-label {
  font-size: 14px;
  color: #999;
}

.charts-row {
  margin-bottom: 20px;
}

.chart-card {
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.card-header {
  font-weight: bold;
  font-size: 16px;
  color: #333;
}

.chart-container {
  width: 100%;
  height: 400px;
}
</style>